import tornado.web
import json
import tornado.httpserver
from datetime import date,datetime
from utils.XcnvsMediaApi import XcnvsMediaApi
from settings import g_logger, g_settings

class ComplexEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)

class HttpController(tornado.web.RequestHandler):

    def initialize(self):
        # 初始化函数，一般用于初始化环境变量
        pass

    def get_request_ip(self):
        request_ip = self.request.headers.get("X-Real-IP") or self.request.headers.get("X-Forwarded-For") or \
                    self.request.remote_ip
        return request_ip

    def get_request_port(self):
        # address = self.request.connection.context.address
        request_port = 20000
        return request_port

    def request_get_params(self):
        params = {}
        for key in self.request.arguments:
            params[key] = self.get_argument(key)
        return params

    def request_post_params(self):
        post_params = self.request.arguments
        post_params = {x: post_params.get(x)[0].decode("utf-8") for x in post_params.keys()}

        # print("获取POST 表单请求参数：",type(post_params),post_params)
        # tornado 接收json方式上传的参数

        if not post_params:
            post_params = self.request.body.decode('utf-8')
            post_params = json.loads(post_params)
            # print("获取POST json请求参数：", type(post_params), post_params)

        return post_params

g_xcnvsMediaApi= XcnvsMediaApi(g_logger,g_settings)
g_xcmsDict = dict() # 所有在线的xcms管理容器
g_messageCmdXcmsStreamList = dict()
g_messageCmdXcmsLiveStart = dict()
g_messageCmdXcmsLiveStop = dict()
g_messageCmdXcmsRecordTimeline = dict()
g_messageCmdXcmsRecordFilename = dict()
g_messageCmdXcmsStreamAddProxy = dict()
g_messageCmdXcmsStreamDelProxy = dict()
g_messageCmdXcmsStreamPtz = dict()


